package com.xbongbong.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.enums.Status;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.statistic.manager.feign.DolphinFeignClient;
import com.xbongbong.pro.statistic.result.pojo.dto.ProjectDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ProjectVO;
import com.xbongbong.util.HeaderUtil;
import com.xbongbong.util.HttpRequestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 处理项目相关的内容
 *
 * @ClassName DealProjects
 * /dolphinscheduler/projects
 * @Author 梁鲁江
 * 创建时间 2022/6/13 20:34
 * 修改时间  2022/6/13 20:34
 **/
@Component
public class ProjectsHelper {
    private static final Log log = LogFactory.getLog(ProjectsHelper.class);
    @Resource
    private DolphinFeignClient dolphinFeignClient;

    /**
     * 分页获取项目名称
     * 
     * @return
     */
    public String queryProjectListPaging(){
        //
        return "";
    }
    /**
     * 创建项目
     * 
     * @return
     */
    public String create(String projectName,String description) throws XbbException {
        JSONObject requestData = new JSONObject();
        requestData.put("projectName",projectName);
        requestData.put("description",description);
        String url = HeaderUtil.getHosts()+"/dolphinscheduler/projects";
        //参数格式要求  description=description&projectName=projectName
        String ret = HttpRequestUtils.post(HeaderUtil.getHeader(), url, requestData.entrySet().stream().map(v->String.join("=",v.getKey(),v.getValue().toString())).collect(Collectors.joining("&")));
        JSONObject result = JSON.parseObject(ret);
        Integer code = result.getInteger("code");
        if(Objects.equals(code, Status.SUCCESS.getCode())){
            //TODO 把 id 存储到数据库，后面根据id删除
            
        }else if(Objects.equals(code, Status.PROJECT_ALREADY_EXISTS.getCode())){
            //TODO 项目已经存在  返回之后要配合业务计数加1存到数据库里  改名之后再次调用此方法    没有提供根据名称查询的方法  ，根据id的也不可用
        }else {
            log.error(ErrorCodeEnum.API_ERROR_263001.getCode() +"___"+result);
            throw  new XbbException(ErrorCodeEnum.API_ERROR_263001);
        }
        System.out.println(ret);
        return "";
    }

    /**
     * 根据项目名称获取项目code
     * @param projectDTO
     * @return
     */
    public Long queryCodeByName(ProjectDTO projectDTO) throws XbbException {
        XbbResponse<Object> diyResponse  = dolphinFeignClient.queryCodeByName(projectDTO);
        if (diyResponse != null ) {
            if(diyResponse.isSuccess()){
                ProjectVO projectVO = JSONObject.parseObject(JSON.toJSONString(diyResponse.getResult(), SerializerFeature.DisableCircularReferenceDetect), ProjectVO.class);
                if(projectVO.getCode() == null){
                    log.error(ErrorCodeEnum.API_ERROR_263001.getCode() +"___"+projectVO.getCode());
                    throw  new XbbException(ErrorCodeEnum.API_ERROR_263001);
                }
                return projectVO.getCode();
            }else {
                log.error(ErrorCodeEnum.API_ERROR_263001.getCode() +"===");
                throw  new XbbException(diyResponse.getCode(),diyResponse.getMsg());
            }
        }
        return 0L;
    }

    /**
     * 根据code删除
     * @param code
     * @return
     */
    public Long delete(Long code){
        String url = HeaderUtil.getHosts()+"/dolphinscheduler/projects/"+code;
        //参数格式要求  description=description&projectName=projectName
        String ret = HttpRequestUtils.delete(HeaderUtil.getHeader(), url);
        JSONObject result = JSON.parseObject(ret);
        Integer resultCode = result.getInteger("code");
        return 0L;
    }
 

/*    public static void main(String[] args) {
        create("项目测试创建","项目测试创建描述");
//        JSONObject requestData = new JSONObject();
//        requestData.put("projectName","projectName");
//        requestData.put("description","description");
//        System.out.println(requestData.entrySet().stream().map(v->String.join("=",v.getKey(),v.getValue().toString())).collect(Collectors.joining("&")));
    }*/
}
